//
//  THDataParserHelper.m
//  THBaseApp
//
//  Created by TryHone on 16/6/3.
//  Copyright © 2016年 TryHone. All rights reserved.
//

#import "JFDataParserHelper.h"
#import "NSDictionary+SafeAccess.h"
#import "NSData+Base64.h"
#import "JFMacro.h"
#import "JFBase.h"
#import "JFBasePri.h"

@implementation JFDataParserHelper

+(JFResultVo*)dataParser:(id)data  dataParserType:(DataParserType)type{
//    id dataTemp=nil;
//    JFResultVo * resustvo=[[JFResultVo alloc] init];
//    resustvo.errorNo=0;
//    switch (type) {
//        case DataParserType_JSON:
//        {
//
//            if([data isKindOfClass:[NSDictionary class]]){
//                NSDictionary * tempDict = (NSDictionary*)data;
//                NSString * errorId = [tempDict stringForKey:@"errorId"];
//                NSString * errorMsg = [tempDict stringForKey:@"errorMsg"];
//                NSString * notifyMsg = [tempDict stringForKey:@"notifyMsg"];
//                NSInteger  code = [tempDict integerForKey:@"code"];
//                NSString * msg = [tempDict stringForKey:@"message"];
//                id  result = [tempDict objectForKey:@"result"];
//                id  updateInfo = [result dictionaryForKey:@"updateInfo"];
//                if (errorId) {//交易接口
//                    if([@"EM0512000000" isEqualToString:errorId]){
//                        resustvo.errorNo = 0;
//                    }else{
//                        resustvo.errorNo = -1;
//                    }
//                    resustvo.noShowErrorInfo = errorMsg;
//                    resustvo.errorInfo = errorMsg;
//                    resustvo.results = result;
//                    if ([result isKindOfClass:[NSDictionary class]]) {
//                        NSMutableDictionary * dict =[[NSMutableDictionary alloc] initWithDictionary:result];
//                        dict[@"notifyMsg"] = notifyMsg;
//                        dict[@"errorId"] = errorId;
//                        resustvo.results = [dict copy];;
//                    }
//
//
//                }else{
//                    resustvo.errorNo = code;
//                    resustvo.noShowErrorInfo = msg;
//                    resustvo.errorInfo = msg;
//                    resustvo.results = result;
//                }
//                if (updateInfo) {
//                    [[NSNotificationCenter defaultCenter] postNotificationName:JFAPPUPDATEINFO object:updateInfo];
//                }
//
//                if (code != 0) {
//
//                    JFMsgCenterVo * msg2 = [[JFMsgCenterVo alloc] init];
//                    msg2.from = @"BASE";
//                    msg2.to = @"APP";
//                    NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
//                    dict[@"action"] = @(code);
//                    dict[@"msg"] = msg;
//                    dict[@"params"] = result;
//                    msg2.requestNum = @"000008";
//                    msg2.params = dict;
//                    [JFMsgCenteManager requestWithMsgCenterVo:msg2 callback:^(JFResultVo *resultVo) {
//
//                    }];
//
//                }
//
//            }else if ([data isKindOfClass:[NSData class]]){
//                dataTemp = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
//
//                if([dataTemp isKindOfClass:[NSDictionary class]]){
//                    NSDictionary * tempDict = (NSDictionary*)dataTemp;
//                    NSInteger  code = [tempDict integerForKey:@"code"];
//                    NSString * msg = [tempDict stringForKey:@"message"];
//                    id  result = [tempDict objectForKey:@"result"];
//                    resustvo.errorNo = code;
//                    resustvo.noShowErrorInfo = msg;
//                    resustvo.errorInfo = msg;
//                    resustvo.results = result;
//                }else{
//                    resustvo.results = dataTemp;
//                }
//
//            }
//
//            return resustvo;
//
//        }
//            break;
//
//        case DataParserType_XML:
//        {
//
//
//        }
//            break;
//        case DataParserType_WebSocket:
//        {
//
//            JFResultVo * resustvo=[[JFResultVo alloc] init];
//            if(data){
//                NSData * tempdata = [NSData uncompressZippedData:data];
//                NSError * error = nil;
//                dataTemp = [NSJSONSerialization JSONObjectWithData:tempdata options:0 error:&error];
//                if(error){
//                    resustvo.errorNo = -999;
//                    resustvo.errorInfo = [error localizedDescription];
//                }else{
//                    resustvo.errorNo = 0;
//
//                }
//                resustvo.results = dataTemp;
//
//            }else{
//                resustvo.errorNo = -999;
//                resustvo.errorInfo = @"未获取到数据";
//                resustvo.results = data;
//            }
//
//
//
//            return resustvo;
//
//        }
//            break;
//
//        default:
//            break;
//    }
//
//    return dataTemp;
   return  [self dataParser:data dataParserType:type requestVo:nil];
}

+(JFResultVo*)dataParser:(id)data  dataParserType:(DataParserType)type requestVo:(JFRequestVo*)requestVo{
    
    id dataTemp=nil;
    JFResultVo * resustvo=[[JFResultVo alloc] init];
    resustvo.errorNo=0;
    switch (type) {
        case DataParserType_JSON:
        {
            
            if([data isKindOfClass:[NSDictionary class]]){
                NSDictionary * tempDict = (NSDictionary*)data;
                NSString * errorId = [tempDict stringForKey:@"errorId"];
                NSString * errorMsg = [tempDict stringForKey:@"errorMsg"];
                NSString * notifyMsg = [tempDict stringForKey:@"notifyMsg"];
                NSInteger  code = [tempDict integerForKey:@"code"];
                NSString * msg = [tempDict stringForKey:@"message"];
                id  result = [tempDict objectForKey:@"result"];
                id  updateInfo = nil;
                if(!result){
                    result = [tempDict objectForKey:@"data"];
                }
                
                if ([result isKindOfClass:[NSDictionary class]]) {
                    updateInfo = [result dictionaryForKey:@"updateInfo"];
                }
                if ([result isKindOfClass:[NSArray class]]) {
                    NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
                    dict[@"data"] = result;
                    result = dict;
                }
               
               
                if (errorId) {//交易接口
                    if([@"EM0512000000" isEqualToString:errorId]){
                        resustvo.errorNo = 0;
                    }else{
                        resustvo.errorNo = -1;
                    }
                    resustvo.noShowErrorInfo = errorMsg;
                    resustvo.errorInfo = [JFStringHelper internationalString:errorMsg];
                    resustvo.results = result;
                    if ([result isKindOfClass:[NSDictionary class]]) {
                        NSMutableDictionary * dict =[[NSMutableDictionary alloc] initWithDictionary:result];
                        dict[@"notifyMsg"] = notifyMsg;
                        dict[@"errorId"] = errorId;
                        resustvo.results = [dict copy];;
                    }
                    resustvo.errorId = [errorId integerValue];
                    
                    
                }else{
                    resustvo.errorNo = code;
                    resustvo.noShowErrorInfo = msg;
                    resustvo.errorInfo = [JFStringHelper internationalString:msg];
                    if( [result isEqual:[NSNull null]]){
                        result = [[NSDictionary alloc] init];
                    }
                    resustvo.results = result;
                }
                if (updateInfo) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:JFAPPUPDATEINFO object:updateInfo];
                }
                  
                if (code != 0 && requestVo && requestVo.forwardErrorCode) {
                    
                    JFMsgCenterVo * msg2 = [[JFMsgCenterVo alloc] init];
                    msg2.from = @"BASE";
                    msg2.to = @"APP";
                    NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
                    dict[@"action"] = @(code);
                    dict[@"msg"] = [JFStringHelper internationalString:msg];
                    dict[@"params"] = result;
                    msg2.requestNum = @"000008";
                    msg2.params = dict;
                    [JFMsgCenteManager requestWithMsgCenterVo:msg2 callback:^(JFResultVo *resultVo) {
                        
                    }];
                    
                }
                
            }else if ([data isKindOfClass:[NSData class]]){
                dataTemp = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
                
                if([dataTemp isKindOfClass:[NSDictionary class]]){
                    NSDictionary * tempDict = (NSDictionary*)dataTemp;
                    NSInteger  code = [tempDict integerForKey:@"code"];
                    NSString * msg = [tempDict stringForKey:@"message"];
                    id  result = [tempDict objectForKey:@"result"];
                    resustvo.errorNo = code;
                    resustvo.noShowErrorInfo = msg;
                    resustvo.errorInfo = [JFStringHelper internationalString:msg];
                    resustvo.results = result;
                }else{
                    resustvo.results = dataTemp;
                }
                
            }
            
            return resustvo;
            
        }
            break;
            
        case DataParserType_XML:
        {
            
            
        }
            break;
        case DataParserType_WebSocket:
        {
            
            JFResultVo * resustvo=[[JFResultVo alloc] init];
            if(data){
                NSData * tempdata = [NSData uncompressZippedData:data];
                NSError * error = nil;
                dataTemp = [NSJSONSerialization JSONObjectWithData:tempdata options:0 error:&error];
                if(error){
                    resustvo.errorNo = -999;
                    resustvo.errorInfo = [error localizedDescription];
                }else{
                    resustvo.errorNo = 0;
                    
                }
                resustvo.results = dataTemp;
                
            }else{
                resustvo.errorNo = -999;
                resustvo.errorInfo = @"未获取到数据";
                resustvo.results = data;
            }
            
            
            
            return resustvo;
            
        }
            break;
            
        default:
            break;
    }
    
    return dataTemp;
}

+(NSString*)errorCodeToErrorMsg:(NSInteger)code{
 
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
    dict[@(-996)] = @"另一进程使用的后台会话";
    dict[@(-997)] = @"后台会话已断开连接";
    dict[@(-998)] = @" 未知错误";
    dict[@(-999)] = @" 错误取消";
    dict[@(-1000)] = @"错误地址";
    dict[@(-1001)] = @"请求超时";
    dict[@(-1002)] = @"不支持的URL";
    dict[@(-1003)] = @"无法找到主机";
    dict[@(-1004)] = @"无法连接到主机";
    dict[@(-1005)] = @"网络连接丢失";
    dict[@(-1006)] = @"DNS查找失败";
    dict[@(-1007)] = @"HTTP重定向过多";
    dict[@(-1008)] = @"资源不可用";
    dict[@(-1009)] = @"未连接到Internet";
    dict[@(-1010)] = @"重定向到不存在的位置";
    dict[@(-1011)] = @"服务器未响应";
    dict[@(-1012)] = @"用户取消身份验证";
    dict[@(-1013)] = @"需要用户身份验证";
    dict[@(-1014)] = @"零字节资源";
    dict[@(-1015)] = @"无法解码原始数据";
    dict[@(-1016)] = @"错误无法解码内容数据";
    dict[@(-1017)] = @" 无法分析响应";
    dict[@(-1018)] = @"国际漫游关闭";
    dict[@(-1019)] = @"激活的CallIs";
    dict[@(-1020)] = @"不允许的数据";
    dict[@(-1021)] = @"请求正文流已耗尽";
    dict[@(-1022)] = @"应用传输安全性要求安全连接";
    dict[@(-1100)] = @"文件不存在";
    dict[@(-1101)] = @"文件目录是错误";
    dict[@(-1102)] = @"没有读取文件的权限";
    dict[@(-1103)] = @"数据长度超过最大值";
    NSNumber * number = [NSNumber numberWithInteger:code];
    NSString * msg = dict[number];
    if ([JFStringHelper isEmpty:msg]) {
        msg = @"请求异常，请重试";
    }
    JFMsgCenterVo * msg2 = [[JFMsgCenterVo alloc] init];
    msg2.from = @"BASE";
    msg2.to = @"APP";
    NSMutableDictionary * dict2 = [[NSMutableDictionary alloc] init];
    dict2[@"action"] = @(code);
    dict2[@"msg"] = [JFStringHelper internationalString:msg];
    msg2.requestNum = @"000008";
    msg2.params = dict2;
    [JFMsgCenteManager requestWithMsgCenterVo:msg2 callback:^(JFResultVo *resultVo) {
        
    }];
    
    return msg;
}
@end
